// eee.Sheffield.PZ.Imaging
//
// Copyright ?Ping Zou, 2007
// sg71.cherub@gmail.com
using System;
using System.Collections.Generic;
using System.Text;
using eee.Sheffield.PZ.Math;
using System.Drawing;
using System.Drawing.Imaging;

namespace eee.Sheffield.PZ.Imaging.ImageFilter
{
    public class GradientYCentralDifference : ImageFilter//, IGraylevelImageFilter
    {       
        #region Fields
        //private string _flag = "[Image -> Gradient -> Gradient Y Central Difference]";        
        #endregion

        //#region Properties
        //public string Log() { return _log; }
        //public string Flag() { return _flag; }
        //#endregion

        #region Constructor
        public GradientYCentralDifference()
        {
            _flag = "[Image -> Gradient -> Gradient Y Central Difference]";
            _normalize = true;
        }
        #endregion

        #region Apply filter
        //public Bitmap ApplyGraylevelImageFilter(Bitmap srcImage) { return Apply(srcImage, out _log); }
        //public PZMath_matrix ApplyDoubleGraylevelImageFilter(PZMath_matrix srcMatrix) { return ApplyDouble(srcMatrix, out _log); }

        //private Bitmap Apply(Bitmap srcImage, out string logText)
        //{
        //    PZMath_matrix srcMatrix = _converter.ConvertGraylevelBitmapToMatrix(srcImage);
        //    PZMath_matrix dstMatrix = ApplyDouble(srcMatrix, out logText);
        //    Bitmap dstImage = _converter.ConvertMatrixToGraylevelBitmap(dstMatrix, true);
        //    return dstImage;
        //}     

        protected override PZMath_matrix ApplyDoubleGraylevelImageFilter(PZMath_matrix srcMatrix, out string logText)
        {
            #region source and dst matrix
            // get source
            int width = srcMatrix.ColumnCount;
            int height = srcMatrix.RowCount;

            // create dstMatrix
            PZMath_matrix dstMatrix = new PZMath_matrix(height, width);
            int heightM1 = height - 1;
            int widthM1 = width - 1;
            int heightM2 = height - 2;
            int widthM2 = width - 2;
            #endregion

            #region apply filter, Y direction
            // for each pixel
            for (int y = 1; y < heightM1; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    dstMatrix[y, x] = (srcMatrix[y + 1, x] - srcMatrix[y - 1, x]) / 2.0;
                }
            }
            #endregion

            #region boundary, upper and bottom boundaries
            // copy bottom column
            for (int x = 0; x < width; x++)
            {
                dstMatrix[0, x] = srcMatrix[1, x] - srcMatrix[0, x];
                dstMatrix[heightM1, x] = srcMatrix[heightM1, x] - srcMatrix[heightM2, x];
            }
            #endregion
            logText = "succeeds.";
            return dstMatrix;

        } // ApplyDoubleGraylevelImageFilter()

        protected override PZMath_matrix[] ApplyDoubleColourImageFilter(PZMath_matrix[] srcMatrixArray, out string logText)
        {
            // TODO: add this function

            throw new Exception("The method or operation is not implemented.");
        } // ApplyDoubleColourImageFilter()
        #endregion
    }
}
